package com.cw.demo.dao.impl;

import com.cw.demo.dao.JobDao;
import com.cw.demo.model.Job;
import com.cw.demo.utils.DbUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class JobDaoImpl implements JobDao {

    public boolean saveJob(Job job){
        Connection connection = DbUtils.getInstance().getConnection();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("insert into job(id, jname, description) values (?,?,?)");
            preparedStatement.setInt(1,job.getId());
            preparedStatement.setString(2,job.getJname());
            preparedStatement.setString(3,job.getDescription());
            preparedStatement.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean updateJob(Job job){

        Connection connection = DbUtils.getInstance().getConnection();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("update job set jname = ? , description = ? where  id = ?");
            preparedStatement.setString(1,job.getJname());
            preparedStatement.setString(2,job.getDescription());
            preparedStatement.setInt(3,job.getId());
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteJob(int id){
        Connection connection = DbUtils.getInstance().getConnection();

        try {
            PreparedStatement preparedStatement = connection.prepareStatement("delete from  job where  id = " + id);
            preparedStatement.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 查询操作。根据查询条件  dname和loc进行模糊查询
     * @param param
     * @return
     */
    public List<Job> findByParam(Job param){
        String sql = "select * from job where 1 = 1";
        //字符串的操作工具类：可以帮助判断字符串是否为null或''
        if(param != null){
            if(StringUtils.isNotEmpty(param.getJname())){
                sql += " and dname like '%"+param.getJname()+"%'";
            }
            if (StringUtils.isNotEmpty(param.getDescription())){
                sql += " and loc like '%"+param.getDescription()+"%'";
            }
        }

        //将查询结果进行重新封装，返回集合bean对象
        Connection connection = DbUtils.getInstance().getConnection();
        //创建一个返回结果集类型的空对象
        List<Job> list = new ArrayList<Job>();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Job job = new Job();
                job.setId(resultSet.getInt("id"));
                job.setJname(resultSet.getString("jname"));
                job.setDescription(resultSet.getString("description"));

                list.add(job);
            }
//            return list;
        } catch (SQLException e) {
            e.printStackTrace();
//            return list;
        }
        return list;
    }


    /**
     * 根据主键ID查询对象信息
     * @param id
     * @return
     */
    public Job findById(int id){
        Connection connection = DbUtils.getInstance().getConnection();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("select  * from job where  id = " + id);
            ResultSet resultSet = preparedStatement.executeQuery();//此时，数据返回的肯定只有一条或0条
            Job job = new Job();
            boolean result = resultSet.next();
            if (result){
                job.setId(resultSet.getInt("id"));
                job.setJname(resultSet.getString("jname"));
                job.setDescription(resultSet.getString("description"));
            }
            return job;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }
}
